package com.tianhua.javs.core.engine.handler;

import com.tianhua.javs.client.bean.JavsTranslateContextBean;
import com.tianhua.javs.client.bean.ScriptVarMappingBean;
import com.tianhua.javs.client.bean.TranslateResultBean;
import com.tianhua.javs.client.enums.SegmentSceneEnum;
import com.tianhua.javs.client.utils.StringHandleUtils;
import com.tianhua.javs.core.constant.TagConstants;
import com.tianhua.javs.core.engine.PipelineTranslator;
import com.tianhua.javs.core.parse.SegmentSpreadParseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Description:
 * date: 2022/2/24
 *
 * @author shenshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
@Service
public class ConstructHandler extends AbstractElementHandler{

    @Autowired
    private SegmentSpreadParseService segmentSpreadParseService;

    @Resource(name = "businessPipelineTranslator")
    private PipelineTranslator businessPipelineTranslator;

    /**
     * 处理构造函数赋值场景
     * @param javsShell
     * @param keyWard
     * @param translateContextBean
     * @return
     */

    public TranslateResultBean handleTranslate(String javsShell, String keyWard, JavsTranslateContextBean translateContextBean, SegmentSceneEnum sceneEnum){
        String newKeyWard = getNewKeWard(keyWard);
        //无参数构造函数
        if(!newKeyWard.contains(".") && StringHandleUtils.isUpperFirst(newKeyWard.trim()) && keyWard.endsWith(TagConstants.BRACKET_ALL)){
            String className = keyWard.split("\\(")[0];
            addRunTimePackage(translateContextBean, className);
            String javaSegment = TagConstants.BLANK_ONE + TagConstants.NEW + keyWard;
            javaSegment = segmentSpreadParseService.getBlankPrefix(javsShell) + className.trim() + TagConstants.BLANK_ONE + javsShell.replace(keyWard,javaSegment)  + TagConstants.ROW_END;
            return TranslateResultBean.getInstance(javaSegment,true);
        }

        //有参构造函数
        if(!newKeyWard.contains(".") && StringHandleUtils.isUpperFirst(newKeyWard.trim()) && keyWard.endsWith(TagConstants.BRACKET_RIGHT)){
            String javaSegment = businessPipelineTranslator.translateKeyWard(translateContextBean,javsShell,keyWard);

            ScriptVarMappingBean scriptVarMappingBean = ScriptVarMappingBean.builder().build();
            if (javsShell.contains(TagConstants.EQ)) {
                String varName = javsShell.split(TagConstants.EQ)[0].trim();
                scriptVarMappingBean.setJavsVarCode(varName);
            }

            String className = keyWard.split("\\(")[0];
            scriptVarMappingBean.setReferClassModel(translateContextBean.getDomainModelBean().searchClass(null, className));
            scriptVarMappingBean.setReferClassName(className);
            scriptVarMappingBean.setVarClassName(className);
            scriptVarMappingBean.setJavaCode(className.substring(0, 1).toLowerCase() + className.substring(1));
            translateContextBean.getCurrentScriptMethodBean().addRunTimeVar(scriptVarMappingBean);
            addRunTimePackage(translateContextBean, className);
            javaSegment = TagConstants.BLANK_ONE + TagConstants.NEW + javaSegment;
            javaSegment =  segmentSpreadParseService.getBlankPrefix(javsShell) + className.trim() + TagConstants.BLANK_ONE + javsShell.replace(keyWard,javaSegment)  + TagConstants.ROW_END;
            return TranslateResultBean.getInstance(javaSegment,true);
        }
        return TranslateResultBean.getInstance(keyWard);
    }

}
